// import { createRouter, createWebHashHistory } from 'vue-router'
import { createRouter, createWebHistory } from 'vue-router'
import Wzlogin from '../views/Wzlogin.vue'
import Wzregister from '../views/Wzregister.vue'



const router = createRouter({
  // history:createWebHashHistory(),
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
      {path:'',component:Wzlogin},
      {path:'/login',component:Wzlogin},
      // 懒加载方式,不会自动加载，当你调用的时候加载
      {path:'/register',component:Wzregister},
      {path:'/control',component:()=>import('../views/Wzcontrol.vue'),
      children:[
        {path:'',component:()=>import('../views/product.vue'),meta: {
          requiresAuth: true // 需要进行身份验证
        }},
        {path:'order',component:()=>import('../views/order.vue'),meta: {
          requiresAuth: true // 需要进行身份验证
        }},
        {path:'product',component:()=>import('../views/product.vue'),meta: {
          requiresAuth: true // 需要进行身份验证
        }},
        {path:'product1',component:()=>import('../views/product1.vue'),meta: {
          requiresAuth: true // 需要进行身份验证
        }},
      ],  meta: {
        requiresAuth: true // 需要进行身份验证
      }
    },
  ]
})


// 路由导航守卫
router.beforeEach((to, from, next) => {
  // 检查 seesionStorage 中是否存在 userInfo
  const token = sessionStorage.getItem('token')
 
   if (to.meta.requiresAuth && !token ) {
    // 如果没有 token 或者 token 无效
    next('/login')
  } else {
    next()
  }
})


export default router
